package com.freshmarket.freshmarket.service.impl;

import com.freshmarket.freshmarket.controller.form.FormKillProduct;
import com.freshmarket.freshmarket.controller.form.KillProduct;
import com.freshmarket.freshmarket.entity.LimitedTimeOffer;
import com.freshmarket.freshmarket.dao.LimitedTimeOfferMapper;
import com.freshmarket.freshmarket.entity.ProductType;
import com.freshmarket.freshmarket.service.ILimitedTimeOfferService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freshmarket.freshmarket.utils.Kill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.DecimalMin;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Squirrel
 * @since 2020-09-04
 */
@Service
public class LimitedTimeOfferServiceImpl extends ServiceImpl<LimitedTimeOfferMapper, LimitedTimeOffer> implements ILimitedTimeOfferService {

    @Autowired
    private LimitedTimeOfferMapper limitedTimeOfferMapper;
    @Override
    public List<ProductType> findNames() {
        return limitedTimeOfferMapper.findNames() ;
    }

    @Override
    public List<FormKillProduct> findByProductTypeId(int productTypeId) {
        return limitedTimeOfferMapper.findByProductTypeId(productTypeId);
    }

    @Override
    public String findByIdNum(int id,int number)  {
        if(id==0){
            return "请先选择商品";
        }
        if(number<=0){
            return "请输入正确的数量";
        }
        int productNumber = limitedTimeOfferMapper.findByIdNum(id);
        if(productNumber<number){
            return "库存不足,当前库存:"+productNumber;
        }
        return null;
    }

    @Transactional
    @Override
    public String addKillProduct(FormKillProduct formKillProduct) {


        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        int number = formKillProduct.getNumber();
        List<Kill> kill = limitedTimeOfferMapper.getNumber(formKillProduct.getProductId());
        int totalNumber=0;
        int index=0;
        int endNumber=0;
        for (int i=0;i<kill.size();i++){
            totalNumber+=kill.get(i).getProductBatchNum();
        }
        if(totalNumber<number){
            return "数量不够";
        }else{
            for (int i=0;i<kill.size();i++){
               if( kill.get(i).getProductBatchNum()>=number){
                     endNumber=number;
               }else {
                   number=number-kill.get(i).getProductBatchNum();
                   index++;
               }
            }
        }
        List<Integer> id=new ArrayList<>();
        for (int i=0;i<index;i++){
           id.add(kill.get(i).getProductBatchId());
        }
        id.add(0);
        limitedTimeOfferMapper.updateNumbers(id);
        limitedTimeOfferMapper.updateNumbe(kill.get(index).getProductBatchId(),endNumber);
        String sqlTime = df.format(new Date());
        List<Integer> ids=new ArrayList<>();
        for (int i=0;i<=index;i++){
            ids.add(kill.get(i).getProductBatchId());
        }
        limitedTimeOfferMapper.insertOperation(ids,sqlTime);
        LimitedTimeOffer limitedTimeOffer = new LimitedTimeOffer();
        limitedTimeOffer.setLimitedTimeOfferDiscount(Double.parseDouble(formKillProduct.getDiscount()));
        limitedTimeOffer.setLimitedTimeOfferDuration(formKillProduct.getTime()+"");
        limitedTimeOffer.setProductId(formKillProduct.getProductId());


        String formatDate = df.format(formKillProduct.getDate());
        limitedTimeOffer.setLimitedTimeOfferStartTime(formKillProduct.getNewDate());

        limitedTimeOffer.setLimitedTimeOfferTotalNum(formKillProduct.getNumber());
        limitedTimeOffer.setLimitedTimeOfferPerNum(1);
        System.out.println(limitedTimeOffer);
        limitedTimeOfferMapper.addKillProduct(limitedTimeOffer);
        limitedTimeOfferMapper.updateNumber(formKillProduct.getProductId(),formKillProduct.getNumber());
        return "添加成功";
    }

    @Override
    public List<KillProduct> findAll() {
        return limitedTimeOfferMapper.findAll();
    }
}
